<template>
  <el-card shadow="never">
    <div slot="header" ref="topHeader">
      <el-form :inline="true" :model="listQuery" v-if="curUserRole == -1">
        <el-form-item label="发行商:">
          <el-input style="width: 160px" v-model="listQuery.name" @keypress.native.enter="getList" placeholder="输入发行商" clearable/>
        </el-form-item> 
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="getList" @keypress.native.enter="getList">查询</el-button>
          <el-button type="success" v-permission="['sys:publisher:add']" icon="el-icon-circle-plus-outline" @click="addEdit(null)">增加</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div v-loading="loading" >
      <el-table ref="tableCot" element-loading-text="请稍后..." :data="tableData" border  >
        <el-table-column  type="index" width="50" label="序号" align="center"/>
        <!-- <el-table-column prop="id" label="发行商ID"  align="center"/> -->
        <el-table-column prop="name" label="发行商" align="center" show-overflow-tooltip/>
        <el-table-column prop="nameCode" label="发行商首字母" align="center"  show-overflow-tooltip />
        <el-table-column prop="createTime" width="160" label="添加时间" align="center" show-overflow-tooltip/>
        <el-table-column fixed="right" label="操作" width="210">
          <template slot-scope="scope">
            <el-tag v-permission="['sys:publisher:update']" style="cursor:pointer;margin-right: 4px;" size="mini" @click="openAddCard(scope.row)">
              <i class="el-icon-tickets"/>生成卡号
            </el-tag>
            <el-tag v-permission="['sys:publisher:update']" style="cursor:pointer;margin-right: 4px;" size="mini" @click="addEdit(scope.row)">
              <i class="el-icon-edit"/>编辑
            </el-tag>
            <el-tag v-permission="['sys:publisher:delete']" style="cursor:pointer;" size="mini" type="danger" @click="remove(scope.row)">
              <i class="el-icon-delete"/>删除
            </el-tag>
          </template>
        </el-table-column>
      </el-table>
      <!--分頁-->
      <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.pageSize" @pagination="getList"/>
      <!--添加/编辑-->
      <add-edit
          :dialog-visible="addEditVisible"
          :data="addEditData"
          @close="closeDialog"
          @reloadTable="reloadTable"
      />
      <!-- 生成卡号弹窗 -->
      <addCard :dialog-visible="cardDialog"
          :data="addEditData" @close="closeDialog"/>
    </div>
  </el-card>
</template>
<script>
import Pagination from '@/components/Pagination'
import addEdit from '@/views/biz/publisher/addEdit'
import addCard from '@/views/biz/publisher/addCard'
import publisherApi from '@/api/system/publisher'

export default {
  name: 'publisher',
  components: {Pagination, addEdit, addCard},
  data() {
    return {
      loading: false,
      addEditVisible: false,
      cardDialog:false,
      total: 0,
      listQuery: {
        page: 1,
        pageSize: 10,
        name: null
      },
      showTable: true,
      tableData: [],
      addEditData: {},
      ctimeList:[],
      curUserRole: -1
    }
  },
  computed: {
    user() {
      return this.$store.state.user
    }
  },
  mounted() {
    this.user.roles.forEach(item => {
      if(item != 1 && item != 2){
        this.curUserRole = 3;
        if( this.user.publisherIds){
          this.listQuery.inPulisherId = this.user.publisherIds
        }else{
          this.listQuery.inPulisherId = ''
        }
      }else{
        this.listQuery.inPulisherId = ''
      }
    })
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      // if(this.curUserRole > -1){
      //   this.listQuery.id = this.user.publisherId;
      // }
      publisherApi.list(this.listQuery).then(res => {
         console.log(res,"1324")
        this.tableData = res.data.data || []
        this.total = res.data.count || 0
        this.loading = false;
      }).catch(() => this.loading = false)
    },
     
    reloadTable(){
      this.getList();
    },
    //添加或修改弹窗
    addEdit(row) {
      if (row) {
        this.addEditData = JSON.parse(JSON.stringify(row))
      }else{
        this.addEditData = {};
      }
      this.addEditVisible = true
    },
    // 生成卡号
    openAddCard(row){
      this.cardDialog = true;
      this.addEditData = row;
    },
    queryDetail(row){
      this.addEditData = JSON.parse(JSON.stringify(row))
      this.addEditVisible = true
    },
    remove(row) {
      this.$confirm('确定要删除该条数据吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        publisherApi.remove(row.id).then(res => {
          if(res.status == 200){
            this.reloadTable()
          }else{
            this.$message.success('该发行商存在卡号不可删除')
          }
        })
      })
    },
    closeDialog() {
      this.addEditVisible = false;
      this.cardDialog = false;
    }
  }
}
</script>
<style scoped lang="scss">

</style>
